package com.fengwk.support.constant;

import java.util.List;

import org.springframework.boot.context.properties.ConfigurationProperties;

import com.fengwk.support.util.ArrayUtils;
import com.fengwk.support.util.CollectionUtils;
import com.fengwk.support.util.MathUtils;
import com.fengwk.support.util.StringUtils;

@Deprecated
//@Component
@ConfigurationProperties("constant.url-pool")
public class UrlPoolConstant {
	
	private static List<String> URL_POOL_USER_CENTER;
	private static List<String> URL_POOL_RESOURCE;
	private static List<String> URL_POOL_EDU;
	private static List<String> URL_POOL_MSG;
	private static List<String> URL_POOL_BLOG;
	private static List<String> URL_POOL_FILE;
	
	private final String URL_POOL_SEPARATOR = ",";
	
	public static String urlUserCenter() {
		return getRandomUrl(URL_POOL_USER_CENTER);
	}

	public static String urlResource() {
		return getRandomUrl(URL_POOL_RESOURCE);
	}

	public static String urlEdu() {
		return getRandomUrl(URL_POOL_EDU);
	}

	public static String urlMsg() {
		return getRandomUrl(URL_POOL_MSG);
	}

	public static String urlBlog() {
		return getRandomUrl(URL_POOL_BLOG);
	}

	public static String urlFile() {
		return getRandomUrl(URL_POOL_FILE);
	}

	public static List<String> urlPoolUserCenter() {
		return URL_POOL_USER_CENTER;
	}

	public static List<String> urlPoolResource() {
		return URL_POOL_RESOURCE;
	}

	public static List<String> urlPoolEdu() {
		return URL_POOL_EDU;
	}

	public static List<String> urlPoolMsg() {
		return URL_POOL_MSG;
	}

	public static List<String> urlPoolBlog() {
		return URL_POOL_BLOG;
	}

	public static List<String> urlPoolFile() {
		return URL_POOL_FILE;
	}

	/*---------------------- 注入方法 --------------------------*/

	public void setUserCenter(String userCenter) {
		URL_POOL_USER_CENTER = buildUrlPool(userCenter);
	}

	public void setResource(String resource) {
		URL_POOL_RESOURCE = buildUrlPool(resource);
	}

	public void setEdu(String edu) {
		URL_POOL_EDU = buildUrlPool(edu);
	}

	public void setMsg(String msg) {
		URL_POOL_MSG = buildUrlPool(msg);
	}

	public void setBlog(String blog) {
		URL_POOL_BLOG = buildUrlPool(blog);
	}

	public void setFile(String file) {
		URL_POOL_FILE = buildUrlPool(file);
	}

	private List<String> buildUrlPool(String urlPoolStr) {
		List<String> urlPool = null;
		if (StringUtils.isNotBlank(urlPoolStr)) {
			String[] urls = urlPoolStr.split(URL_POOL_SEPARATOR);
			urlPool = ArrayUtils.toList(urls);
		}
		return urlPool;
	}

	private static String getRandomUrl(List<String> urlPool) {
		String url = null;
		if (CollectionUtils.isNotBlank(urlPool)) {
			int limit = urlPool.size();
			int index = MathUtils.getRandomBetweenInteger(0, limit - 1);
			url = urlPool.get(index);
		}
		return url;
	}
	
}
